package com.jwt.demo.sys.service.impl;

import com.jwt.demo.sys.dao.UserDao;
import com.jwt.demo.sys.entity.User;
import com.jwt.demo.sys.mapper.UserMapper;
import com.jwt.demo.sys.service.TokenService;
import com.jwt.demo.sys.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 林武杰
 * @since 2021-10-08
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private TokenService tokenService;

    @Override
    public String login(User user) {
        User resultUser = userDao.getByUsername(user.getUsername());
        if (ObjectUtils.isEmpty(resultUser))
        {
            return "无此账号";
        }
        else {
            if (StringUtils.equals(user.getPassword(),resultUser.getPassword())){
                String token = tokenService.getToken(resultUser);
                return token;
            }
            else {
                return "密码错误";
            }
        }
    }
}
